﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;

namespace XMM.Controls.Library
{
    /// <summary>
    /// Interaktionslogik für Import_Episode.xaml
    /// </summary>
    public partial class Import_Episode : UserControl
    {
        /// <summary>
        /// Reference to Parent ImportMedia()-Dialog.
        /// </summary>
        private ImportMedia ParentMediaImport { get; set; }

        /// <summary>
        /// Currently Selected MediaInfo()-Object.
        /// </summary>
        public Objects.MediaInfo SelectedMediaInfo { get; set; }

        /// <summary>
        /// List of existing Shows.
        /// </summary>
        public ObservableCollection<Objects.Series> _AvailableSeries = new ObservableCollection<Objects.Series>();

        /// <summary>
        /// Referer to list of existing Shows - used for cmbSeries.
        /// </summary>
        private ObservableCollection<Objects.Series> AvailableSeries
        {
            get
            {
                return _AvailableSeries;
            }
        }

        /// <summary>
        /// Currently selected show.
        /// </summary>
        Objects.Series SelectedShow { get; set; }

        /// <summary>
        /// Currently selected Season.
        /// </summary>
        Objects.SeriesSeason SelectedSeason { get; set; }

        /// <summary>
        /// List of existing Seasons for the selected show.
        /// </summary>
        public ObservableCollection<Objects.SeriesSeason> _AvailableSeasons = new ObservableCollection<Objects.SeriesSeason>();

        /// <summary>
        /// Referer to list of existing Seasons - used for cmbSeasons.
        /// </summary>
        private ObservableCollection<Objects.SeriesSeason> AvailableSeasons
        {
            get
            {
                return _AvailableSeasons;
            }
        }

        /// <summary>
        /// Movie()-Data for the currently selected MediaInfo().
        /// </summary>
        public Objects.SeriesEpisode EpisodeData { get; set; }

        public Objects.SeriesEpisode CurrentData
        {
            get
            {
                Objects.SeriesEpisode oCurrentData = new Objects.SeriesEpisode();

                oCurrentData.AudioInfo = ctlAudioInfo.CurrentAudioInfo;
                if (ctlDiskMedia.SelectedDisk != null)
                    oCurrentData.DiskMediaID = ctlDiskMedia.SelectedDisk.DiskMediaID;
                else
                    oCurrentData.DiskMediaID = 0;

                try
                {
                    oCurrentData.EpisodeNo = int.Parse(txtEpisodeNumber.Text);
                }
                catch (Exception)
                {
                    oCurrentData.EpisodeNo = 0;
                }

                oCurrentData.MediaFile = SelectedMediaInfo.FullPath;
                oCurrentData.Name = txtEpisodeTitle.Text;
                oCurrentData.Plot = ctlPlot.Plot;
                if (cmbSeason.SelectedIndex > -1)
                    oCurrentData.SeasonID = ((Objects.SeriesSeason)cmbSeason.SelectedItem).SeasonID;
                else
                    oCurrentData.SeasonID = 0;

                if (cmbSeries.SelectedIndex > -1)
                    oCurrentData.SeriesID = ((Objects.Series)cmbSeries.SelectedItem).SeriesID;
                else
                    oCurrentData.SeriesID = 0;

                oCurrentData.VideoInfo = ctlVideoInfo.CurrentVideoInfo;
                oCurrentData.HDLevel = oCurrentData.VideoInfo.HDLevel;

                return oCurrentData;
            }
        }


        private Mutex mutOnlineUpdate;

        public Import_Episode(ImportMedia dlgImportMedia, Objects.MediaInfo oSelectedMediaInfo)
        {
            InitializeComponent();

            SelectedMediaInfo = oSelectedMediaInfo;
            ParentMediaImport = dlgImportMedia;
            mutOnlineUpdate = new Mutex();
        }

        Dialoges.Library.ImportFile ParentImportFile;
        Dialoges.Library.ImportDisc ParentImportDisc;

        public Import_Episode(Dialoges.Library.ImportFile dlgImportFile, Objects.MediaInfo oSelectedMediaInfo)
        {
            InitializeComponent();

            SelectedMediaInfo = oSelectedMediaInfo;
            mutOnlineUpdate = new Mutex();

            btnSaveChanges.IsEnabled = false;
            btnSaveChanges.Visibility = System.Windows.Visibility.Collapsed;
            btnApplyForAll.IsEnabled = false;
            btnApplyForAll.Visibility = System.Windows.Visibility.Collapsed;

            // Prefetch Shows.
            ParentImportFile = dlgImportFile;
            _AvailableSeries = dlgImportFile.AvailableSeries;
        }

        public Import_Episode(Dialoges.Library.ImportDisc dlgImportDisc, Objects.MediaInfo oSelectedMediaInfo)
        {
            InitializeComponent();

            SelectedMediaInfo = oSelectedMediaInfo;
            mutOnlineUpdate = new Mutex();

            btnSaveChanges.IsEnabled = false;
            btnSaveChanges.Visibility = System.Windows.Visibility.Collapsed;
            btnApplyForAll.IsEnabled = false;
            btnApplyForAll.Visibility = System.Windows.Visibility.Collapsed;
            ctlDiskMedia.IsEnabled = false;
            ctlDiskMedia.Visibility = System.Windows.Visibility.Collapsed;

            // Prefetch Shows.
            ParentImportDisc = dlgImportDisc;
            _AvailableSeries = dlgImportDisc.AvailableSeries;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            ctlAudioInfo.SelectedFilePath = SelectedMediaInfo.FullPath;
            ctlVideoInfo.SelectedFilePath = SelectedMediaInfo.FullPath;

            // Load Shows.
            loadShows();

            // Check if SelectedMediaInfo contains Episode-Details.
            if (SelectedMediaInfo.UserdefinedMediaInfo != null)
            {
                Objects.SeriesEpisode oEpisode = (Objects.SeriesEpisode)SelectedMediaInfo.UserdefinedMediaInfo;

                if (oEpisode != null)
                {
                    Objects.Series oSelectedShow = null;
                    if (oEpisode.SeriesID != 0)
                        oSelectedShow = new Objects.Series(oEpisode.SeriesID);
                    else
                        oSelectedShow = new Objects.Series();

                    cmbSeries.SelectedItem = oSelectedShow;
                    cmbSeries.Text = oSelectedShow.Name;

                    Objects.SeriesSeason oSelectedSeason = null;
                    if (oEpisode.SeasonID != 0)
                        oSelectedSeason = new Objects.SeriesSeason(oEpisode.SeasonID);
                    else
                        oSelectedSeason = new Objects.SeriesSeason();

                    // Try to get Episode-No. from filename.
                    if (oEpisode.EpisodeNo == 0)
                    {
                        oEpisode.EpisodeNo = Functions.StringManipulation.getEpisodeNoFromFileName(SelectedMediaInfo.FileName);
                        txtEpisodeNumber.Text = oEpisode.EpisodeNo.ToString();
                    }
                    else
                    {
                        txtEpisodeNumber.Text = oEpisode.EpisodeNo.ToString();
                    }


                    cmbSeason.SelectedItem = oSelectedSeason;
                    cmbSeason.Text = "Staffel " + oSelectedSeason.Number;

                    txtEpisodeNumber.Text = oEpisode.EpisodeNo.ToString();
                    txtEpisodeTitle.Text = oEpisode.Name;
                    if (oEpisode.EpisodeNo == 0 || oEpisode.SeasonID == 0 || oEpisode.SeriesID == 0)
                        txtEpisodeTitle.Text = SelectedMediaInfo.FullPath.Split('\\')[SelectedMediaInfo.FullPath.Split('\\').Count() - 1];

                    ctlAudioInfo.CurrentAudioInfo = oEpisode.AudioInfo;
                    ctlAudioInfo.loadInfo();
                    ctlVideoInfo.CurrentVideoInfo = oEpisode.VideoInfo;
                    ctlVideoInfo.loadInfo();
                    ctlDiskMedia.setDiskMedia(new Objects.DiskMedia(oEpisode.DiskMediaID));
                    ctlDiskMedia.setStatusIcon();

                    if (oEpisode.Plot != String.Empty)
                        ctlPlot.setPlot(oEpisode.Plot);
                }
            }
            else
            {
                if (ParentImportFile != null)
                {
                    Objects.SeriesEpisode oEpisode = new Objects.SeriesEpisode();
                    if (SelectedMediaInfo.MediaType != 2 || SelectedMediaInfo.UserdefinedMediaInfo != null)
                        oEpisode = (Objects.SeriesEpisode)SelectedMediaInfo.UserdefinedMediaInfo;
                    else
                    {
                        oEpisode.Name = SelectedMediaInfo.FileName;
                        oEpisode.MediaFile = SelectedMediaInfo.FullPath;
                        //oEpisode.AudioInfo = new Objects.AudioInfo();
                        //ctlAudioInfo.AnalyseAudio();
                    }
                    

                    Objects.Series oSelectedShow = null;
                    if (oEpisode.SeriesID != 0)
                        oSelectedShow = new Objects.Series(oEpisode.SeriesID);
                    else
                        oSelectedShow = new Objects.Series();

                    cmbSeries.SelectedItem = oSelectedShow;
                    cmbSeries.Text = oSelectedShow.Name;

                    Objects.SeriesSeason oSelectedSeason = null;
                    if (oEpisode.SeasonID != 0)
                        oSelectedSeason = new Objects.SeriesSeason(oEpisode.SeasonID);
                    else
                        oSelectedSeason = new Objects.SeriesSeason();

                    // Try to get Episode-No. from filename.
                    if (oEpisode.EpisodeNo == 0)
                    {
                        oEpisode.EpisodeNo = Functions.StringManipulation.getEpisodeNoFromFileName(SelectedMediaInfo.FileName);
                        txtEpisodeNumber.Text = oEpisode.EpisodeNo.ToString();
                    }
                    else
                    {
                        txtEpisodeNumber.Text = oEpisode.EpisodeNo.ToString();
                    }


                    cmbSeason.SelectedItem = oSelectedSeason;
                    cmbSeason.Text = "Staffel " + oSelectedSeason.Number;

                    txtEpisodeNumber.Text = oEpisode.EpisodeNo.ToString();
                    txtEpisodeTitle.Text = oEpisode.Name;
                    if (oEpisode.EpisodeNo == 0 || oEpisode.SeasonID == 0 || oEpisode.SeriesID == 0)
                        txtEpisodeTitle.Text = SelectedMediaInfo.FullPath.Split('\\')[SelectedMediaInfo.FullPath.Split('\\').Count() - 1];

                    if (oEpisode.AudioInfo != null && oEpisode.AudioInfo.Channels > 0)
                    {
                        ctlAudioInfo.CurrentAudioInfo = oEpisode.AudioInfo;
                        ctlAudioInfo.loadInfo();
                    }
                    else
                    {
                        ctlAudioInfo.AnalyseAudio();
                    }

                    if (oEpisode.VideoInfo != null && oEpisode.VideoInfo.VideoTracks > 0)
                    {
                        ctlVideoInfo.CurrentVideoInfo = oEpisode.VideoInfo;
                        ctlVideoInfo.loadInfo();
                    }
                    else
                    {
                        ctlVideoInfo.AnalyseVideo();
                    }

                    ctlDiskMedia.setDiskMedia(new Objects.DiskMedia(oEpisode.DiskMediaID));
                    ctlDiskMedia.setStatusIcon();

                    if (oEpisode.Plot != String.Empty)
                        ctlPlot.setPlot(oEpisode.Plot);
                }
            }
        }

        public void loadShows()
        {
            /*
            _AvailableSeries = new ObservableCollection<Objects.Series>();
            List<Objects.Series> lSeries = Objects.Series.getAllSeries();
            if (lSeries != null && lSeries.Count > 0)
            {
                foreach (Objects.Series oSeries in lSeries)
                {
                    _AvailableSeries.Add(oSeries);
                }
            }

            cmbSeries.ItemsSource = AvailableSeries;
            cmbSeries.Items.Refresh();

            _AvailableSeasons = new ObservableCollection<Objects.SeriesSeason>();
            cmbSeason.ItemsSource = _AvailableSeasons;
            cmbSeason.Items.Refresh();
             * */

            if (ParentMediaImport != null)
                _AvailableSeries = ParentMediaImport._AvailableSeries;
            if (ParentImportFile != null)
                _AvailableSeries = ParentImportFile.AvailableSeries;

            cmbSeries.ItemsSource = AvailableSeries;
            cmbSeries.Items.Refresh();
        }

        public void reloadShows(Objects.Series oShow)
        {
            if(ParentMediaImport != null)
                ParentMediaImport.startPrefetchShows();

            _AvailableSeries.Add(oShow);
            cmbSeries.Items.Refresh();
        }

        public void loadSeasons()
        {
            _AvailableSeasons = new ObservableCollection<Objects.SeriesSeason>();

            if (cmbSeries.SelectedIndex > -1 && cmbSeries.SelectedItem != null)
            {
                SelectedShow = (Objects.Series)cmbSeries.SelectedItem;

                List<Objects.SeriesSeason> lSeasons = Objects.Series.getSeriesSeasons(SelectedShow.SeriesID);

                if (lSeasons != null && lSeasons.Count > 0)
                {
                    foreach (Objects.SeriesSeason oSeason in lSeasons)
                    {
                        _AvailableSeasons.Add(oSeason);
                    }
                }
            }
            else
            {
                SelectedShow = null;
                SelectedSeason = null;
            }

            cmbSeason.ItemsSource = AvailableSeasons;
            cmbSeason.Items.Refresh();
        }

        private void cmbSeries_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            loadSeasons();
        }

        private void cmbSeason_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbSeason.SelectedIndex > -1 && cmbSeason.SelectedItem != null)
            {
                SelectedSeason = (Objects.SeriesSeason)cmbSeason.SelectedItem;
                //txtEpisodeNumber.Text = (SelectedSeason.Episodes.Count + 1).ToString();
            }
        }

        private void btnAddShow_Click(object sender, RoutedEventArgs e)
        {
            Dialoges.Library.AddTVShow dlgTVShowCreator = new Dialoges.Library.AddTVShow(this);
            dlgTVShowCreator.ShowDialog();
        }

        private void btnAddSeason_Click(object sender, RoutedEventArgs e)
        {
            Dialoges.Library.AddShowSeason dlgSeasonCreator = new Dialoges.Library.AddShowSeason(this);
            dlgSeasonCreator.ShowDialog();
        }

        private void btnSaveChanges_Click(object sender, RoutedEventArgs e)
        {
            if (txtEpisodeNumber.Text != String.Empty && cmbSeason.SelectedIndex > -1 && cmbSeries.SelectedIndex > -1)
            {
                try
                {
                    int iEpisodeNumber = int.Parse(txtEpisodeNumber.Text);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Verwenden Sie für die Episoden-Nr. nur Zahlen und Ziffern!", "Ungültige Episoden-Nr.", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    return;
                }

                SaveChanges();
            }
            else if(txtEpisodeNumber.Text == String.Empty)
                MessageBox.Show("Bitte geben Sie eine Nummer für diese Episode ein!", "Keine Episoden-Nr.", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            else if(cmbSeries.SelectedIndex == -1)
                MessageBox.Show("Bitte wählen Sie eine Serie und Staffel aus!", "Keine Serie", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            else if(cmbSeason.SelectedIndex == -1)
                MessageBox.Show("Bitte wählen Sie eine Staffel aus!", "Keine Staffel", MessageBoxButton.OK, MessageBoxImage.Exclamation);
        }

        private void SaveChanges()
        {
            EpisodeData = new Objects.SeriesEpisode();
            EpisodeData.MediaFile = SelectedMediaInfo.FullPath;
            EpisodeData.AudioInfo = ctlAudioInfo.CurrentAudioInfo;
            EpisodeData.VideoInfo = ctlVideoInfo.CurrentVideoInfo;
            EpisodeData.Name = txtEpisodeTitle.Text;
            EpisodeData.SeriesID = SelectedShow.SeriesID;
            EpisodeData.SeasonID = SelectedSeason.SeasonID;
            EpisodeData.Plot = ctlPlot.Plot;
            EpisodeData.EpisodeNo = int.Parse(txtEpisodeNumber.Text);
            EpisodeData.DiskMediaID = 0;
            if (ctlDiskMedia.SelectedDisk != null && ctlDiskMedia.SelectedDisk.DiskMediaID != 0)
            {
                EpisodeData.DiskMediaID = ctlDiskMedia.SelectedDisk.DiskMediaID;
            }

            SelectedMediaInfo.UserdefinedMediaInfo = EpisodeData;
            SelectedMediaInfo.IsEdited = true;

            if (ParentMediaImport != null)
            {
                ParentMediaImport.updateSelected(SelectedMediaInfo);
                ParentMediaImport.lvFoundItems.Items.Refresh();
            }
        }

        private delegate void SaveChangesCallback();

        private void btnOnlineUpdate_Click(object sender, RoutedEventArgs e)
        {
            if (SelectedShow != null && SelectedSeason != null && txtEpisodeNumber.Text != String.Empty)
            {
                setStatus("Lade Episoden-Details von TheTVDB.com...", true);

                int iEpisodeNumber = 0;

                try
                {
                    iEpisodeNumber = int.Parse(txtEpisodeNumber.Text);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Bitte geben Sie für die Nummer der Folge nur Zahlen und Ziffern ein!", "Fehlerhafte Eingabe", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    return;
                }

                ParameterizedThreadStart tsOnlineUpdate = new ParameterizedThreadStart(doOnlineUpdate);
                Thread thOnlineUpdate = new Thread(tsOnlineUpdate);
                thOnlineUpdate.Start(new object[] { SelectedShow, SelectedSeason, iEpisodeNumber });
            }
            else
            {
                MessageBox.Show("Bitte wählen Sie zuerst eine Serie und Staffel aus und geben Sie die Nummer der Folge ein!", "Fehlende Daten", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            }
        }

        private void doOnlineUpdate(object Data)
        {
            if (mutOnlineUpdate.WaitOne(1000))
            {
                // Data-Array-Order: Objects.Series oSelectedShow, Objects.SeriesSeason oSelectedSeason, int iEpisodeNumber
                object[] oaData = (object[])Data;

                Objects.Series oSelectedShow = (Objects.Series)oaData[0];
                Objects.SeriesSeason oSelectedSeason = (Objects.SeriesSeason)oaData[1];
                int iEpisodeNumber = (int)oaData[2];

                // Load Settings.
                Settings oSettings = new Settings();

                // Get Episode-Info.
                TvdbLib.Data.TvdbEpisode vEpisode = tvdbInterface.tvdbInterface.getEpisodeInfo(oSettings.LibraryDataStorage + "tvdb\\cache", oSelectedShow.TvdbID, oSelectedSeason.Number, iEpisodeNumber);
                if (vEpisode != null)
                {
                    /*
                    txtEpisodeTitle.Text = vEpisode.EpisodeName;
                    txtEpisodeTitle.FontStyle = FontStyles.Italic;
                    ctlPlot.setPlot(vEpisode.Overview);
                     * */
                    Dispatcher.Invoke(new updateGUICallback(updateGUI), new object[] { vEpisode.EpisodeName, vEpisode.Overview });
                    Dispatcher.Invoke(new SaveChangesCallback(SaveChanges));
                }

                mutOnlineUpdate.ReleaseMutex();
            }
            else
            {
                MessageBox.Show("Es läuft bereits eine Online-Abfrage! Bitte warten Sie das Ergebnis der laufenden Abfrage ab.", "Online-Abfrage aktiv", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            }

            //            setStatus("Lade Episoden-Details von TheTVDB.com...", true);
            Dispatcher.Invoke(new setStatusCallback(setStatus), new object[] { "Bereit...", false });
        }

        private void updateGUI(string sEpisodeName, string sPlot)
        {
            txtEpisodeTitle.Text = sEpisodeName;
            txtEpisodeTitle.FontStyle = FontStyles.Italic;
            ctlPlot.setPlot(sPlot);
        }

        private delegate void updateGUICallback(string sEpisodeName, string sPlot);

        private void btnApplyForAll_Click(object sender, RoutedEventArgs e)
        {
            /*
            if (SelectedShow != null && SelectedShow.SeriesID > 0)
            {
                if (MessageBox.Show("Möchten Sie wirklich die Medieninfos für alle nicht bearbeiten Dateien von diesem Eintrag übernehmen? Die Serien-Nummerierung wird dabei nicht automatisch generiert!", "Serie für alle Dateien übernehmen?", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
                {
                    if (ParentMediaImport.FileCollection != null && ParentMediaImport.FileCollection.Count > 0)
                    {
                        int iCurrentFile = 1;

                        Objects.SeriesEpisode oTemplateEpisode = new Objects.SeriesEpisode();
                        oTemplateEpisode.SeriesID = SelectedShow.SeriesID;
                        if (SelectedSeason != null && SelectedSeason.Number != 0)
                            oTemplateEpisode.SeasonID = SelectedSeason.SeasonID;
                        oTemplateEpisode.DiskMediaID = ctlDiskMedia.SelectedMediaID;
                        oTemplateEpisode.EpisodeNo = 0;

                        int iUpdatedRecords = 0;

                        for (int i = 0; i < ParentMediaImport.FileCollection.Count; i++)
                        {
                            // Check if MediaInfo-Record is unedited.
                            if (ParentMediaImport.FileCollection[i].UserdefinedMediaInfo == null)
                                ParentMediaImport.FileCollection[i].UserdefinedMediaInfo = new Objects.SeriesEpisode();

                            if (ParentMediaImport.FileCollection[i].IsEdited == false && ParentMediaImport.FileCollection[i].UserdefinedMediaInfo != null && ParentMediaImport.FileCollection[i].UserdefinedMediaInfo.GetType() == typeof(Objects.SeriesEpisode))
                            {
                                // Inject required values.
                                Objects.SeriesEpisode oEpisode = (Objects.SeriesEpisode)ParentMediaImport.FileCollection[i].UserdefinedMediaInfo;
                                oTemplateEpisode.MediaFile = ParentMediaImport.FileCollection[i].FileName;
                                if (oEpisode != null)
                                {
                                    oTemplateEpisode.EpisodeNo = 0; // Functions.StringManipulation.getEpisodeNoFromFileName(ParentMediaImport.FileCollection[i].FileName);
                                    oTemplateEpisode.AudioInfo = oEpisode.AudioInfo;
                                    oTemplateEpisode.VideoInfo = oEpisode.VideoInfo;
                                    oTemplateEpisode.Name = oEpisode.Name;
                                }
                                else
                                {
                                    oTemplateEpisode.EpisodeNo = 0;
                                    oTemplateEpisode.AudioInfo = new Objects.AudioInfo();
                                    oTemplateEpisode.VideoInfo = new Objects.VideoInfo();
                                    oTemplateEpisode.Name = String.Empty;
                                }

                                // Apply new Details.
                                ParentMediaImport._FileCollection[i].MediaType = 2;
                                ParentMediaImport._FileCollection[i].IsEdited = true;
                                ParentMediaImport._FileCollection[i].checkNewFile();
                                ParentMediaImport._FileCollection[i].UserdefinedMediaInfo = oTemplateEpisode;
                                ++iCurrentFile;

                                ++iUpdatedRecords;
                            }

                            ParentMediaImport.lvFoundItems.Items.Refresh();
                        }

                        MessageBox.Show(iUpdatedRecords + " Dateien aktualisiert", "Vorlage übernommen", MessageBoxButton.OK, MessageBoxImage.Information);
                     * 
                     * 
                    }
                }
            }
            else
            {
                MessageBox.Show("Bitte wählen Sie zuerst eine Serie für diese Datei!", "Keine Serie", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            }
                 */

            if (MessageBox.Show("Möchten Sie die Basis-Infos für alle nicht bearbeiteten Objekte übernehmen?", "Für alle übernehmen?", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
            {

                SaveChanges();
                Objects.SeriesEpisode SelectedEpisode = (Objects.SeriesEpisode)SelectedMediaInfo.UserdefinedMediaInfo;

                if (SelectedEpisode.SeriesID > 0 && SelectedEpisode.SeasonID > 0 && SelectedEpisode.MediaFile != String.Empty)
                {
                    ParentMediaImport.ApplyForAll(SelectedEpisode);
                }
                else if (SelectedEpisode.SeriesID == 0)
                {
                    MessageBox.Show("Bitte wählen Sie zuerst eine Serie aus!", "Keine Serie ausgewählt", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                }
                else if (SelectedEpisode.SeasonID == 0)
                {
                    MessageBox.Show("Bitte wählen Sie zuerst eine Staffel aus!", "Keine Staffel ausgewählt", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                }
                else if (SelectedEpisode.MediaFile == String.Empty)
                {
                    MessageBox.Show("Keine Datei ausgewählt!", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }

        private void setStatus(string sNewMessage, bool bEnableMarquee)
        {
            if (ParentMediaImport != null)
            {
                ParentMediaImport.lblStatus.Content = sNewMessage;
                ParentMediaImport.pbStatus.IsIndeterminate = bEnableMarquee;
            }
        }

        private delegate void setStatusCallback(string sNewMessage, bool bEnableMarquee);
    }
}
